package cn.iune.erp.customer;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


@Transactional
@Service
public class CustomerServiceImpl implements ICustomerService {
	
	@Autowired
	private IFileCustomerService fileCustomerService;
	@Autowired
	private IFileCustomerAddressService fileAddressService;
	@Autowired
	private IMemberPointsService memberPointsService;

	
	
	//删除客户档案及关联的地址明细表
	@Override
	public void deleteCustomer(String rowID){
		//删除从表
		fileAddressService.deleteByMasterID(rowID);		
		//然后删除主表
		fileCustomerService.delete(rowID);
	}
	
	@Override
	public int saveCustomer(JSONObject object) throws Throwable{
		//保存主表
		fileCustomerService.save(object);
		//保存从表
		JSONArray slaves = object.getJSONArray("slaves");
		JSONObject data = slaves.getJSONObject(0);
		fileAddressService.save(data);
		return 0;
	}
	
	
	//注册客户信息（根据登录名）
	@Override
	public JSONObject creatAutomember(String sLoginName, String sMobilePhone, String personID, String personCode, String personName, String cUsercode,String cUsername,String userId,String enterpriseNo,String enterpriseName) {
		JSONObject json = new JSONObject();
		
		FileCustomer fileCustomer = fileCustomerService.creatAutomember(sLoginName, sMobilePhone, personID, personCode, personName, cUsercode, cUsername, userId, enterpriseNo, enterpriseName);
		if(fileCustomer==null) {
			json.put("code", -1);
			json.put("msg", "failure");
		}else {
			json.put("code", 0);
			json.put("msg", "success");
			
			JSONObject data = new JSONObject();
			data.put("customerInfo", fileCustomer);

			json.put("data", data);
		}
		return json;
	}
	
	//获取客户信息（根据登录名）。网上商城/网上外卖使用（包含线上优惠券信息）
	//如果客户不存在，则返回非会员代码和随机ID
	@Override
	public JSONObject getCustomerInfo(String sLoginName, String enterpriseNo) {
		FileCustomer obj = fileCustomerService.getByLoginName(sLoginName, enterpriseNo);
		return this.wrapInfo(obj,"online");
	}

	
	//获取客户信息（根据会员号）。线下零售终端使用（包含线下优惠券信息）
	//如果客户不存在，则返回非会员代码和随机ID
	@Override
	public JSONObject getMemberInfo(String memberNo, String enterpriseNo) {
		FileCustomer obj = fileCustomerService.getByMemberNo(memberNo, enterpriseNo);
		return this.wrapInfo(obj,"offline");
	}

	private JSONObject wrapInfo(FileCustomer obj, String lineFlag) {
		
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		
		//如果对象不存在，可以考虑创建一个客户
		if(obj==null) {
//			json.put("code", -1);
//			json.put("msg", "failure");
//			json.put("info","对象不存在");
//			return json;
			
			//obj = fileCustomerService.creatAutomember(sLoginName, personID, personCode, personName, cUsercode, cUsername, userId, enterpriseNo, enterpriseName)
			obj = fileCustomerService.getTempGuestInfo();
			
			JSONObject data = new JSONObject();
			data.put("customerInfo", obj);
			
			json.put("data", data);

			return json;
		}
		
		JSONObject data = new JSONObject();
		data.put("customerInfo", obj);

		//不能再这里附加单品券和优惠券等信息。
		
		json.put("data", data);
		
		return json;
	}
	
	//获取会员信息（根据登录名）。会员中心使用（不包含优惠券信息）
	@Override
	public JSONObject getMember(String sLoginName, String enterpriseNo) {
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		
		FileCustomer obj = fileCustomerService.getByLoginName(sLoginName, enterpriseNo);
		
		//如果对象不存在，可以考虑创建一个客户
		if(obj==null) {
			json.put("code", -1);
			json.put("msg", "failure");
			json.put("info","您还不是会员！");
			return json;
		}
		
		JSONObject data = new JSONObject();
		data.put("memberInfo", obj);

		if(obj.getIsMember()==0) {
			//是会员
			json.put("code", -2);
			json.put("msg", "failure");
			json.put("info","您还不是会员！");
			return json;
		}
		
		json.put("data", data);
		
		return json;
	}


	//获取会员列表
	@Override
	public JSONObject getCustomerList(String sLoginName) {
		JSONObject json = new JSONObject();
		
		List<FileCustomer> list = fileCustomerService.getList(sLoginName);

		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", list);
		
		return json;
	}


	//积分记录插入之后
	@Override
	public void memberPointsAfterInsert(MemberPoints obj) {
		
		String customerID = obj.getCustomerID();
		if(customerID!=null){
			BigDecimal memberPoints = obj.getMemberPoints();
			fileCustomerService.updateMemberPoints(customerID, memberPoints);
		}
		
	}

	//积分记录删除之前
	@Override
	public void memberPointsBeforeDelete(String fID) {
		
		MemberPoints obj = memberPointsService.getByID(fID);
		String customerID = obj.getCustomerID();
		if(customerID!=null){
			BigDecimal memberPoints = obj.getMemberPoints();
			fileCustomerService.updateMemberPoints(customerID, memberPoints.negate());
		}
	}
	
	
	

}
